package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import entities.ProveedorBean;

public class ProveedorDAO {
	
	public List<dto.Proveedor> proveedoresMasMateriales()
	{
		List<dto.Proveedor> x = new ArrayList<dto.Proveedor>();
		SessionFactory sf = hbt.HibernateUtil.getSessionFactory();
		Session s = sf.openSession();
		
		@SuppressWarnings("unchecked")
		List<entities.ProveedorBean> provMasMat = 
				s.createQuery(" from ProveedorBean p "
						+ "where p.codigo in (select p.clave.proveedor.codigo "
						+ "						from PrecioBean p "
						+ "						group by p.clave.proveedor.codigo "
						+ "						having count(*) >= ALL (select count(*) "
						+ "												from PrecioBean p "
						+ "												group by p.clave.proveedor.codigo))").list();
		
		for(ProveedorBean p : provMasMat)
			x.add(p.toDTO());
		
		return x;
	}	
}
